package com.example.shopping.controller;
import cn.hutool.core.util.StrUtil;
import com.example.shopping.entity.User;
import com.example.shopping.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author xuan 2021/9/16
 */
@Controller
public class UserController {
   @Autowired
   UserService userService;

   @RequestMapping(value = "/showUser", method = {RequestMethod.GET})
   @ResponseBody
   public List<User> showUsers() {
      return userService.showUsers();
   }

   @RequestMapping(value = "/selectUser", method = {RequestMethod.POST})
   @ResponseBody
   public User selectUser(@RequestParam Integer id) {
      return userService.selectUser(id);
   }

   @RequestMapping(value = "/login", method = {RequestMethod.POST})
   @ResponseBody
   public String login(@RequestParam String username,@RequestParam String password) {

      if (username == null || password == null) {
         throw new IllegalArgumentException("参数异常");
      }
      if (!(username instanceof String) || !(password instanceof String)) {
         throw new IllegalArgumentException("参数异常");
      }
      if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
         throw new IllegalArgumentException("参数异常");
      }
      if (StrUtil.containsBlank(username) || StrUtil.containsBlank(password)) {
         throw new IllegalArgumentException("参数异常");
      }
      User user = userService.fondUserByUsername(username);
      if (user == null) {
         return "该用户不存在";
      }
       user = userService.login(username, password);

      if (user == null) {
         return "账号或密码错误";
      }

      return "登入成功！！！！" + "\n" + user;
   }

   @RequestMapping(value = "/register", method = {RequestMethod.POST})
   @ResponseBody
   public String register(String username, String password, String nickname) {
      if (username == null || password == null || nickname == null) {
         throw new  IllegalArgumentException("参数错误");
      }
      if (!(username instanceof String) || !(password instanceof String) || !(nickname instanceof  String)){
         throw new  IllegalArgumentException("参数错误");
      }
      if (username.isEmpty() || password.isEmpty() || nickname.isEmpty()) {
         throw new  IllegalArgumentException("参数错误");
      }
      User user = userService.fondUserByUsername(username);
      if (user != null) {
         return "用户名已存在！！！！";
      }
      userService.register(username, password, nickname);
      return "注册成功";
   }

   @RequestMapping(value = "/updateNicknameAndPassword", method = {RequestMethod.POST})
   @ResponseBody
   public String updateNicknameAndPassword(String username, String password, String nickname, String newPassword) {

      User user = userService.fondUserByUsername(username);
      if (user == null) {
         return "用户不存在";
      }
      if (!user.password.equals(password)) {
         return "用户名或密码错误！！！！";
      }
      userService.updateNicknameAndPassword(username, password, nickname, newPassword);
      return "修改昵称和密码成功";
   }

}
